/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package persistence.dao;

import java.io.Serializable;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import model.Milestone;
import org.apache.commons.logging.Log;
import persistence.SysSql;
import persistence.interfaces.IMilestoneDao;
import persistence.services.BaseDao;
import persistence.services.PersistenceException;
import utils.BaseModel;
import utils.SmLogFactory;

/**
 *
 * @author jmoura
 */
public class MilestoneDao extends BaseDao implements IMilestoneDao, Serializable {

    private static final Log log = SmLogFactory.getLog(MilestoneDao.class);

    @Override
    public Milestone get(int id) throws PersistenceException {
        return (Milestone) super.get(id, SysSql.SelectMilestoneById);
    }

    @Override
    public List<Milestone> list() throws PersistenceException {
        log.trace("Loading Milestones:");
        List<BaseModel> loadList = super.list(SysSql.SelectMilestoneList, "id_milestone");
        //convert back to the desired Class
        //TODO: there must be a better way to cast the entire List in asingle operation.
        List<Milestone> retList = new ArrayList<>();
        for (BaseModel elem : loadList) {
            retList.add((Milestone) elem);
        }
        return retList;
    }

    @Override
    protected Milestone loadColumns(ResultSet rs) throws SQLException, PersistenceException {
        Milestone milestone;
        milestone = new Milestone();
        milestone.setId(rs.getInt("id_milestone"));
        milestone.setName(rs.getString("nm_milestone"));
        milestone.setActive(rs.getString("fl_active").equals("Yes"));
        return milestone;
    }

    @Override
    public void save(Milestone milestone) throws PersistenceException {
        throw new UnsupportedOperationException("Not supported yet.");
    }
}
